import { createStore } from 'vuex'

// Create a new store instance.
const store = createStore({
  //设置全局数据的地方
  state: {
    count: 0,
    message:"helloworld",
    postList:[],
    user: JSON.parse(localStorage.getItem('user'))
  },
  getters: {
     totalPrice(state){
       return state.count*100
     },
     getUser (state) {
      return state.user
    },
  },
  //修改状态的地方,同步
  mutations: {
    setCount (state) {
      state.count++
    },
    setCountNum(state,num){
      state.count+=num
    },
    //通过actions异步设置
    setMessage(state, value){
      state.message = value;
    },
    //通过actions异步设置
    setPostList(state, list){
      state.postList = list;
    },
    //设置用户登录信息
    setUser(state,user){
      console.log("setUser:",user)
      localStorage.setItem('user',JSON.stringify(user))
      state.user = user
    }
  },
  //ajax修改状态的地方,异步
  actions: {
    setMessage(context,value){
      context.commit('setMessage',value);
    },
    setPostList(context,list){
      context.commit('setPostList',list);
    }
  },
  modules: {

  }
})

export default store